
from openai import OpenAI

from ..models.message_input_model import Message
client = OpenAI(
    api_key="sk-yIjuOGeAHkAyavZbHFyx7Ct0ChuQYH7DfKDatmhJ2k55WB9G",
    base_url="https://api.moonshot.cn/v1"
)


##当前发送的问题不需要添加问题
def get_assistant_response(input_content, conversation_history):
    # 如果 conversation_history 为 None，则初始化为空列表
    if conversation_history is None:
        conversation_history = []

    # 添加用户输入到对话记录中
    conversation_history.append(Message("user", input_content).to_dict())

    try:
        completion = client.chat.completions.create(
            model="moonshot-v1-8k",
            messages=conversation_history,
            temperature=0.3,
        )
        result = completion.choices[0].message.content
        # conversation_history.append({
        #     "role": "assistant",
        #     "content": result
        # })
        return result

    except Exception as e:
        print(f"Error getting assistant response: {e}")
        return "Sorry, I couldn't understand that."
